# -*- coding: utf-8 -*-
"""
通知系统模块 - 数据模型
"""

from django.db import models
from django.conf import settings


class Notification(models.Model):
    """
    通知模型
    
    系统通知、活动提醒、互动消息等
    """
    
    # 通知类型
    TYPE_CHOICES = [
        ('system', '系统通知'),
        ('activity', '活动通知'),
        ('comment', '评论通知'),
        ('like', '点赞通知'),
        ('follow', '关注通知'),
        ('message', '私信通知'),
        ('points', '积分通知'),
    ]
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='notifications',
        verbose_name='接收用户'
    )
    
    type = models.CharField(
        max_length=20,
        choices=TYPE_CHOICES,
        verbose_name='通知类型'
    )
    
    title = models.CharField(
        max_length=100,
        verbose_name='通知标题'
    )
    
    content = models.TextField(
        verbose_name='通知内容'
    )
    
    # 关联对象
    related_activity = models.ForeignKey(
        'activities.Activity',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='notifications',
        verbose_name='关联活动'
    )
    
    related_user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='sent_notifications',
        verbose_name='关联用户',
        help_text='触发通知的用户'
    )
    
    # 是否已读
    is_read = models.BooleanField(
        default=False,
        verbose_name='是否已读'
    )
    
    read_at = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='阅读时间'
    )
    
    # 跳转链接
    link_url = models.CharField(
        max_length=200,
        blank=True,
        verbose_name='跳转链接'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间'
    )
    
    class Meta:
        db_table = 'notifications'
        verbose_name = '通知'
        verbose_name_plural = '通知'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user', 'is_read', '-created_at']),
        ]
    
    def __str__(self):
        return f"{self.user.username} - {self.title}"
